/*
 * Copyright 2010 Original Author(s)
 * 
 * This file is part of Kommando
 * 
 * Kommando is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Kommando is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Kommando.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.kommando.core.osgi;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

import org.kommando.core.action.Action;
import org.kommando.core.catalog.ObjectSource;
import org.kommando.core.util.Assert;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

/**
 * Abstract base {@link BundleActivator} with support for registering services.
 * 
 * @author Peter De Bruycker
 */
public abstract class BaseActivator implements BundleActivator {

    protected void registerAction(BundleContext context, Action action) throws Exception {
        Assert.argumentNotNull("context", context);
        Assert.argumentNotNull("action", action);

        context.registerService(Action.class.getName(), action, null);
    }

    protected void registerObjectSource(BundleContext context, ObjectSource objectSource) throws Exception {
        Assert.argumentNotNull("context", context);
        Assert.argumentNotNull("objectSource", objectSource);

        context.registerService(ObjectSource.class.getName(), objectSource, null);
    }

    protected String prepareFile(BundleContext context, String name) throws IOException {
        Assert.argumentNotNull("context", context);
        Assert.argumentNotEmpty("name", name);

        // fetch the file
        File file = context.getDataFile(name);
        // if (!file.exists()) {
        // copy the entry to the file
        URL resource = context.getBundle().getEntry(name);

        InputStream in = null;
        OutputStream out = null;
        try {
            in = resource.openStream();
            out = new FileOutputStream(file);

            copy(in, out);
        } finally {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
        }
        // }

        return file.getAbsolutePath();
    }

    // TODO move this code to some IOUtils class
    static final int BUFF_SIZE = 100000;
    static final byte[] buffer = new byte[BUFF_SIZE];

    public static void copy(InputStream in, OutputStream out) throws IOException {
        while (true) {
            synchronized (buffer) {
                int amountRead = in.read(buffer);
                if (amountRead == -1) {
                    break;
                }
                out.write(buffer, 0, amountRead);
            }
        }
    }
}
